﻿using System.Windows;
using DSPL.Services;
using DSPL.Contracts;
using DSPL.Common;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;

namespace DSPL.Finac.ViewModel
{
    public class SalesRepMasterViewModel : BaseViewModel
    {
        private bool isRepCodeFocused;
        private bool isRepNameFocused;
        private bool isRepIdNoFocused;
        private bool isRepAddressFocused;

        #region Define properties
        public bool IsRepCodeFocused
        {
            get { return isRepCodeFocused; }
            set
            {
                isRepCodeFocused = value;
                this.OnPropertyChanged("IsRepCodeFocused");
            }
        }

        public bool IsRepNameFocused
        {
            get { return isRepNameFocused; }
            set
            {
                isRepNameFocused = value;
                this.OnPropertyChanged("IsRepNameFocused");

            }
        }

        public bool IsRepIdNoFocused
        {
            get { return isRepIdNoFocused; }
            set
            {
                isRepIdNoFocused = value;
                this.OnPropertyChanged("IsRepIdNoFocused");

            }
        }

        public bool IsRepAddressFocused
        {
            get { return isRepAddressFocused; }
            set
            {
                isRepAddressFocused = value;
                this.OnPropertyChanged("IsRepAddressFocused");

            }
        }

        #endregion


        #region Constructor

        public SalesRepMasterViewModel()
            : base()
        {
            MessageTitle = "Sales Representative Master";
        }

        #endregion
        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
        protected override void PreDataValidateExecuted(object obj)
        {
            IsRepCodeFocused = false;
            IsRepNameFocused = false;
            IsRepIdNoFocused = false;
            IsRepAddressFocused = false;

            var messageService = new MessageService();
            if (CurrentRow["RepCode"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.RepCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsRepCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["RepName"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.RepNameNotEmpty, MessageTitle, MessageType.InfoOK);
                IsRepNameFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["RepIdNo"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.RepIdNoNotEmpty, MessageTitle, MessageType.InfoOK);
                IsRepIdNoFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["RepAdd1"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.RepAddressNotEmpty, MessageTitle, MessageType.InfoOK);
                IsRepAddressFocused = true;
                this.IsValid = false;
                return;
            }


            this.IsValid = true;
        }

        protected override void RefreshExternalDataObjectsExecueted(object obj)
        {

            base.RefreshExternalDataObjectsExecueted(obj);

            if (FormMode == Modes.New)
            {
                CurrentRow["Active"] = 1;
                OnPropertyChanged("CurrentRow");

            }

        }

    }
}